package atag.gui;
import java.io.File;
import javax.swing.*;
import java.io.*;

public class Helper {

  public static String getFileName(boolean dirOnly, String prompt,
                                   String defaultName,
                                   String approveBT, JFrame m_parent) {
    JFileChooser fd = new JFileChooser(new File(defaultName));
    fd.setDialogTitle(prompt);
    if (dirOnly)
      fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //File f = new File(new File("c:\\atag-demo\\atag").getCanonicalPath());
    int returnVal = fd.showDialog(m_parent, approveBT);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File curFile = fd.getSelectedFile();
      return curFile.getAbsolutePath();
    }
    return null;
  }

  public static String[] splitFileIntoStrings(File fileName) {
    StringBuffer contents = new StringBuffer();
    String sContents;
    BufferedReader input = null;
    try {
      input = new BufferedReader(new FileReader(fileName));
      String line = null;
      while ( (line = input.readLine()) != null) {
        contents.append(line);
        contents.append(" ");
      }
    }
    catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    finally {
      try {
        if (input != null) {
          input.close();
        }
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    sContents = contents.toString();
    return sContents.split(" ");
  }

}
